package com.demoworld.demo3;

import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.GraphQLError;
import graphql.Scalars;
import graphql.schema.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static graphql.Scalars.GraphQLID;
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLInterfaceType.newInterface;
import static graphql.schema.GraphQLObjectType.newObject;

/**
 * Created by francis on 2017/8/16.
 */
public class Demo3 {
    public static void main(String argsp[]) {
        GraphQLObjectType beanVo = newObject().name("zy")
                .field(newFieldDefinition().name("id").type(Scalars.GraphQLInt).build())//类型一定要与结构类字段的类型相对应
                .field(newFieldDefinition().name("xy").type(Scalars.GraphQLInt).build())
                .field(newFieldDefinition().name("code").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("title").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("jc").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("pic").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("general").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("text").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("readnum").type(Scalars.GraphQLInt).build())
                .field(newFieldDefinition().name("user").type(getUserType()).dataFetcher(new UserDataFetcher()).build())
                .build();

        GraphQLFieldDefinition findforid = GraphQLFieldDefinition.newFieldDefinition().name("zy")
                .argument(//这里用GraphQL些查询语句时传入的参数
                        newArgument()
                                .name("id")//参数名为id
                                .type(Scalars.GraphQLInt)//参数类型
                                .build()
                )
                .type(beanVo)//绑定GraphQL的一个结构，就是上面的那段代码
                .dataFetcher(environment -> {
                    // 获取查询参数
                    int id = environment.getArgument("id");
                    ZyProxy zyProxy = new ZyProxy();//一个对ZyVo数据进行增删改查代理的操作类
                    ZyVo vo = null;
                    try {
                        vo = zyProxy.findById(id);//通过ID查找数据
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return vo;
                }).build();

        GraphQLSchema schema = GraphQLSchema.newSchema()
                .query(newObject().name("GraphQuery")
                        .field(findforid)//半桶水不知道怎么描述它
                        .build()
                )
                .build();

        String ghql = "{zy(id:1){id,title,readnum,text,pic,jc,user{id,name}}}";
        //最后就是获取结果了
        ExecutionResult re = new GraphQL(schema).execute(ghql);
        Map<String, Object> result1 = (Map<String, Object>) re.getData();
        List<GraphQLError> tmpErrors = re.getErrors();
        System.out.println((tmpErrors != null && !tmpErrors.isEmpty()) ? tmpErrors.get(0).toString() : "");
        //把它打印出来
        System.out.println(result1);//结果：{zy={id=1, title=服装与服饰设计, readnum=0, text=null, pic=null, jc=null}}
        //可以把它加入到json对象中
    }

    private static GraphQLObjectType getUserType() {
        return newObject()
                .name("User")
                .field(newFieldDefinition()
                        .name("id")
                        .type(GraphQLID)
                        .dataFetcher(dfe -> ((User) dfe.getSource()).getId()))
                .field(newFieldDefinition()
                        .name("age")
                        .type(GraphQLString)
                        .dataFetcher(dfe -> ((User) dfe.getSource()).getAge()))
                .field(newFieldDefinition()
                        .name("name")
                        .type(GraphQLString)
                        .dataFetcher(dfe -> ((User) dfe.getSource()).getName()))
                .build();
    }
}
